package org.carshared.sqlite;

import java.util.ArrayList;

import org.carshared.utils.PassengerBeen;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapterPassenger {

	// Campos de la BD
	public static final String COLUMN_ID = "id";
	public static final String COLUMN_ID_VIAJE = "idViaje";
	public static final String COLUMN_NOMBRE = "nombre";
	public static final String COLUMN_ACUMULADO = "acumulado";
	public static final String COLUMN_PAGADO = "pagado";

	private static final String DATABASE_TABLE_PASSENGER = "Passenger";
	private Context context;
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;

	public DBAdapterPassenger(Context context) {
		this.context = context;
	}

	public DBAdapterPassenger open() throws SQLException {
		dbHelper = new MySQLiteHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Crea una nueva tarea, si esto va bien retorna la rowId de la tarea, de lo
	 * contrario retorna -1
	 */
	public long createPassenger(PassengerBeen passenger) {
		open();
		ContentValues initialValues = createContentValues(passenger);
		long i = database.insert(DATABASE_TABLE_PASSENGER, null, initialValues);
		close();
		return i;
	}

	// Actualiza la tarea
	public int updatePassenger(PassengerBeen passenger) {
		open();
		ContentValues updateValues = createContentValues(passenger);

		int i = database.update(
				DATABASE_TABLE_PASSENGER,
				updateValues,
				COLUMN_NOMBRE + "=" + "'" + passenger.getNombrePasajero() + "'"
						+ " AND " + COLUMN_ID_VIAJE + "=" + "'"
						+ passenger.getIdViaje() + "'", null);
		close();
		return i;
	}

	// Borra la tarea
	public int deletePassenger(String rowId) {
		open();
		int i = database.delete(DATABASE_TABLE_PASSENGER, COLUMN_ID_VIAJE + "="
				+ "'" + rowId + "'", null);
		close();
		return i;
	}

	// Returna un Cursor que contiene todos los items
	public ArrayList<PassengerBeen> fetchAllPassenger(String rowId) {
		open();
		Cursor mCursor = database.query(DATABASE_TABLE_PASSENGER, new String[] {
				COLUMN_ID, COLUMN_ID_VIAJE, COLUMN_NOMBRE, COLUMN_ACUMULADO,
				COLUMN_PAGADO }, COLUMN_ID_VIAJE + "=" + "'" + rowId + "'",
				null, null, null, null);
		return cursorToArrayList(mCursor);
	}

	// Returna un Cursor que contiene la info del item
	public PassengerBeen fetchPassenger(String rowId) throws SQLException {
		open();
		Cursor mCursor = database
				.query(true, DATABASE_TABLE_PASSENGER, new String[] {
						COLUMN_ID, COLUMN_ID_VIAJE, COLUMN_NOMBRE,
						COLUMN_ACUMULADO, COLUMN_PAGADO }, COLUMN_ID_VIAJE
						+ "=" + "'" + rowId + "'", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		PassengerBeen p = new PassengerBeen(mCursor.getString(1),
				mCursor.getString(2), mCursor.getFloat(3), mCursor.getInt(4));

		return p;
	}

	private ArrayList<PassengerBeen> cursorToArrayList(Cursor c) {

		ArrayList<PassengerBeen> result = new ArrayList<PassengerBeen>();
		while (c.moveToNext()) {
			result.add(new PassengerBeen(c.getString(1), c.getString(2), c
					.getFloat(3), c.getInt(4)));
		}
		close();
		return result;
	}

	private ContentValues createContentValues(PassengerBeen passenger) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ACUMULADO, passenger.getAcumulado());
		values.put(COLUMN_ID_VIAJE, passenger.getIdViaje());
		values.put(COLUMN_NOMBRE, passenger.getNombrePasajero());
		values.put(COLUMN_PAGADO, passenger.isPagado());

		return values;
	}

}
